<template>
	<view class="bg-white">
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="content">新增</block>
		</cu-custom>
		<view style="padding: 15rpx;">
			<view class="action" style="margin-bottom: 10px;">
				<text class="cuIcon-titles " style="color: #1E5EFF;"></text>
				<text style="color: #000000;font-weight: bold;font-size: 25rpx;">
					基础信息填写
				</text>
			</view>
			<view>

				<view>
					<image style="width: 100rpx;height: 100rpx;" :src="dataObj.avatar"></image>
				</view>
				<view class="cu-form-group">
					<view class="title">姓名</view>
					<!--					<my-select range-key="text" value-key="value" dict-code="sys_user,realname,id" v-model="userId" />-->
					<my-seach-select range-key="text" value-key="value" dict-code="sys_user,realname,id"
						v-model="userId" />
				</view>
				<view class="cu-form-group" style="background-color: #E2E2E2;border-radius: 15px;margin-top: 15rpx;">
					<view class="title">工号</view>
					<input v-model="dataObj.workNumber" placeholder="请输入" name="input"
						style="text-align:right;font-size: 20rpx;" disabled />
				</view>
				<view class="cu-form-group" style="background-color: #E2E2E2;border-radius: 15px;margin-top: 15rpx;">
					<view class="title">公司</view>
					<input v-model="dataObj.company_dictText" placeholder="请输入" name="input"
						style="text-align:right;font-size: 20rpx;" disabled />
				</view>

				<view class="cu-form-group" style="background-color: #E2E2E2;border-radius: 15px;margin-top: 15rpx;">
					<view class="title">职务</view>
					<input v-model="dataObj.post" placeholder="请输入" name="input"
						style="text-align:right;font-size: 20rpx;" disabled />
				</view>
				<view class="cu-form-group" style="background-color: #E2E2E2;border-radius: 15px;margin-top: 15rpx;">
					<view class="title">部门名称</view>
					<input v-model="dataObj.departIds_dictText" placeholder="请输入" name="input"
						style="text-align:right;font-size: 20rpx;" disabled />
				</view>
				<view class="cu-form-group" style="background-color: #F4F4F4;border-radius: 15px;margin-top: 15rpx;">
					<view class="title">标题</view>
					<input v-model="title" placeholder="请输入" name="input" style="text-align:right;font-size: 25rpx;">
				</view>
				<view class="cu-form-group" style="background-color: #F4F4F4;border-radius: 15px;margin-top: 15rpx;">
					<view class="title">单据号</view>
					<input v-model="documentNum" placeholder="请输入" name="input"
						style="text-align:right;font-size: 25rpx;">
				</view>
			</view>
		</view>


		<view style="padding: 15rpx;margin-top: 10rpx;">
			<view class="action" style="margin-bottom: 10px;">
				<text class="cuIcon-titles " style="color: #1E5EFF;"></text>
				<text style="color: #000000;font-weight: bold;font-size: 25rpx;">
					体检报告附件上传
				</text>
			</view>
			<view style="margin-top: 30rpx;" class="example-body">
				<uni-file-picker limit="1" file-mediatype="image" @select="select" @delete="deleteFile"></uni-file-picker>
			</view>
		</view>



		<view style="padding: 0 0 30rpx 15rpx;">
			<view style="display: flex;">
				<!-- <view @click="submit"
					style="padding: 30rpx 55px; margin-left: 20rpx;margin-top: 20rpx; background-color: rgba(30, 94, 255, 1);border-radius: 30rpx;color: #FFFFFF;font-size: 10px;margin-right: 5px;">
					提交
				</view> -->
				<view @click="submit"
					style="padding: 30rpx 55px; margin-left: 220rpx;margin-top: 20rpx; background-color: rgba(30, 94, 255, 1);border-radius: 30rpx;color: #FFFFFF;font-size: 10px;margin-right: 5px;">
					提交
				</view>
			</view>
		</view>
		<uni-popup ref="submitFlag" background-color="#fff" :mask-click="false">
			<view
				style="text-align: center;background-color: #FFFFFF;padding: 11rpx;width: 580rpx;height: 470rpx;border-radius: 42rpx;">
				<view>
					<view class="submit-item">
						<image class="submit-icon" src="@/static/duihaoblue.png" />
					</view>
					<view style="margin-top: 28rpx;">
						<text class="submit-text">体检报告已经提交</text>
					</view>
					<view style="color: rgba(150, 150, 150, 1); margin-top: 23rpx;">
						<text>已成功提交</text>
					</view>
					<button @click="sureOne" style="border-radius: 20rpx; background-color: rgba(69, 145, 254, 1);
								color: rgba(255, 255, 255, 1);width: 300rpx;margin-top: 27rpx;">确定</button>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="draftsFlag" background-color="#fff" :mask-click="false">
			<view>
				<view
					style="text-align: center;background-color: #FFFFFF;padding: 11rpx;width: 580rpx;height: 470rpx;border-radius: 42rpx;">
					<view class="submit-item">
						<image class="submit-icon" src="@/static/drafts.png" />
					</view>
					<view style="margin-top: 28rpx;">
						<text class="submit-text">已保存进草稿箱</text>
					</view>
					<view style="color: rgba(150, 150, 150, 1); margin-top: 23rpx;">
						<text>您可以进入草稿箱查看</text>
					</view>
					<button @click="sureThree" style="border-radius: 20rpx; background-color: rgba(69, 145, 254, 1);
				color: rgba(255, 255, 255, 1);width: 300rpx;margin-top: 27rpx;">确定</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import moment from 'moment';
	export default {
		data() {
			return {
				id: Math.floor(Math.random() * 10000000000000000000) + Date.now(),
				userId: '',
				contractType: 2,
				createTime: '',
				title: '',
				documentNum: '',
				intervalTime: '',
				contractFilesList: [],
				contractFiles: [],
				contractSupplementsList: [],
				contractSupplements: [],
				dataObj: {
					avatar: '',
					name: '',
					company: '',
					sex: '',
					idCard: '',
					idCardAddress: '',
					workNumber: '',
					phone: '',
					age: '',
					birthday: '',
					inTime: '',
					workAge: '',
					post: '',
					contractRenewals: [{
						contractStartTime: '',
						contractEndTime: '',
						details: '',
					}]
				},
			}
		},
		computed: {
			createTimeShow: function() {
				return moment(this.createTime).isValid() ? moment(this.createTime).format('MM-DD') : moment().format(
					'MM-DD')
			},
			endTimeShow: function() {
				return moment(this.endTime).isValid() ? moment(this.endTime).format('MM-DD') : moment().format(
					'MM-DD')
			},
			signTimeShow: function() {
				return moment(this.signTime).isValid() ? moment(this.signTime).format('MM-DD') : moment().format(
					'MM-DD')
			}
		},
		watch: {
			userId: {
				immediate: true,
				handler(val, old) {
					console.log("dataObject.userId值变化")
					console.log(val)
					console.log(old)
					this.userId = val;
					this.queryUserByUserId()

				},
			},
			createTime: {
				immediate: true,
				handler(val, old) {
					this.count()
				},
			},
			endTime: {
				immediate: true,
				handler(val, old) {
					this.count()
				},
			}
		},
		mounted() {

		},
		methods: {
			queryUserByUserId() {
				this.$http.get('/crm_user/crmUser/queryById', {
					params: {
						id: this.userId
					}
				}).then(res => {
					console.log("res", res)
					if (res.data.success) {
						let data = res.data.result
						this.dataObj.company_dictText = data.company_dictText;
						this.dataObj.company = data.company;
						this.dataObj.sex_dictText = data.sex_dictText;
						this.dataObj.sex = data.sex;
						this.dataObj.idCard = data.idCard;
						this.dataObj.idCardAddress = data.idCardAddress;
						this.dataObj.avatar = data.avatar;
						this.dataObj.workNumber = data.workNo;
						this.dataObj.realname = data.realname;
						this.dataObj.departIds_dictText = data.departIds_dictText;
						this.dataObj.departIds = data.departIds;
						this.dataObj.inTime = data.proposedEmploymentDate != null ? data.proposedEmploymentDate :
							'';
						this.dataObj.workAge = data.seniority;
						this.dataObj.post = data.post;
						//设置默认合同类型
						if ((this.dataObj.sex == 1 && this.dataObj.age >= 60) || (this.dataObj.sex == 2 && this
								.dataObj.age >=
								40 && this.dataObj.postType == 1) || (this.dataObj.sex == 2 && this.dataObj.age >=
								40 &&
								this.dataObj.postType == 2)) {
							this.contractType = 1
						} else {
							this.contractType = 2
						}
						this.dataObj.contractRenewals = data.contractRenewals.length > 0 ? data.contractRenewals :
							[{
								contractStartTime: '',
								contractEndTime: '',
								details: '',
							}]
					}
				}).catch(err => {
					console.log(err);
				});
			},
			RadioChange(e) {
				this.contractType = e.detail.value
			},
			DateChange(e) {
				this.createTime = e.detail.value
			},
			count() {
				if (this.endTime != '' && this.createTime != '') {
					var date1 = new Date(this.createTime);
					var date2 = new Date(this.endTime);
					var date = (date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24); /*不用考虑闰年否*/
					if (date >= 0) {
						this.intervalTime = date
					} else {
						this.$tip.toast('开始时间不能大于结束时间')
						this.intervalTime = ''
					}
				}
			},
			DateChangeEndTime(e) {
				this.endTime = e.detail.value
			},
			DateChangeSignTime(e) {
				this.signTime = e.detail.value
			},
			submit() {
				const that = this
				this.uploadFileOne(this.contractFilesList)
				//this.uploadFileTwo(this.contractSupplementsList);
				
				setTimeout(() => {
					let params = {
						"medicalReportSecs": this.contractFiles,
						"company": this.dataObj.company,
						"departIds": this.dataObj.departIds,
						"documentNum": this.documentNum,
						"id": this.id,
						"post": this.dataObj.post,
						"realname": this.dataObj.realname,
						"title": this.title,
						"userId": this.userId,
						"workNo": this.dataObj.workNumber
					}
					console.log('params',params)
					this.$http.post('/crm_medical_report/crmMedicalReport/add',params).then(res => {
						console.log('res',res)
						if (res.data.success) {
							this.$refs.submitFlag.open('center')
						} else {
							this.$tip.toast(res.data.message)
						}

					}).catch(() => {
						this.$tip.loaded();
						this.$tip.toast('提交失败')
					});
				}, 500)
			},
			sureOne() {
				this.$refs.submitFlag.close()
				uni.navigateTo({
					url: '/pages/medicals/index'
				})
			},
			save() {
				this.uploadFileOne(this.contractFilesList);
				this.uploadFileTwo(this.contractSupplementsList);
				setTimeout(() => {
					this.$http.post('/crm_contract/crmContract/add', {
						"approvalStatus": "1",
						"contractEndTime": this.endTime + " 00:00:00",
						"contractFiles": this.contractFiles,
						"contractNo": Math.floor(Math.random() * 10000000000000000000) + Date.now(),
						"contractSigningTime": this.signTime + " 00:00:00",
						"contractStartTime": this.createTime + " 00:00:00",
						"contractSupplements": this.contractSupplements,
						"contractType": this.contractType,
						"daysFromSigning": this.intervalTime,
						"id": this.id,
						"userId": this.userId,
						"signType": "1",
						"age": this.dataObj.age,
						"birthday": this.dataObj.birthday,
						"company": this.dataObj.company,
						"enterFactoryTime": this.dataObj.enterFactoryTime,
						"idCard": this.dataObj.idCard,
						"phone": this.dataObj.phone,
						"post": this.dataObj.post,
						"seniority": this.dataObj.workAge,
						"sex": this.dataObj.sex,
						"workNo": this.dataObj.workNumber,
					}).then(res => {
						if (res.data.success) {
							this.$refs.draftsFlag.open('center')
						} else {
							this.$tip.toast(res.data.message)
						}
					}).catch(() => {
						this.$tip.loaded();
						this.$tip.toast('提交失败')
					});
				}, 500)
			},
			sureThree() {
				this.$refs.draftsFlag.close()
				uni.navigateTo({
					url: '/pages/contractList/contractListDrafts/index'
				})
			},
			// 获取上传状态
			select(e) {
				console.log('选择文件：', e)
				// this.imageValue.push(e.tempFilePaths[0])
				this.contractFilesList.push(e.tempFilePaths[0])
			},
			deleteFile(e) {
				console.log('移除文件：', e)
				this.contractFilesList.splice(e.tempFilePaths, 1)
			},
			// 获取上传状态
			selectTwo(e) {
				console.log('选择文件：', e)
				// this.imageValue.push(e.tempFilePaths[0])
				this.contractSupplementsList.push(e.tempFilePaths[0])
			},
			deleteFileTwo(e) {
				console.log('移除文件：', e)
				this.contractSupplementsList.splice(e.tempFilePaths, 1)
			},
			uploadFileOne(fileList) {
				const that = this
				fileList.forEach((item) => {
					that.$http.upload(that.$config.apiUrl + '/sys/common/upload', {
						filePath: item,
						name: 'file'
					}).then(res => {
						that.contractFiles.push({
							medicalReport: res.data.message,
							// medicalReportId: Math.floor(Math.random() * 10000000000000000000) +
							// 	Date
							// 	.now(),
							// id: Math.floor(Math.random() * 10000000000000000000) + Date.now()
						})
					}).catch(err => {
						that.$tip.error(err.data.message)
					});
				})
			},
			uploadFileTwo(fileList) {
				const that = this
				fileList.forEach((item) => {
					that.$http.upload(that.$config.apiUrl + '/sys/common/upload', {
						filePath: item,
						name: 'file'
					}).then(res => {
						that.contractSupplements.push({
							contractSupplementFile: res.data.message,
							contractId: Math.floor(Math.random() * 10000000000000000000) + Date
								.now(),
							id: Math.floor(Math.random() * 10000000000000000000) + Date.now()
						})
					}).catch(err => {
						that.$tip.error(err.data.message)
					});
				})
			}
		}
	}
</script>

<style>
	.cu-form-group {
		background-color: #ffffff;
		line-height: 15rpx;
		padding: 0 15px;
		display: flex;
		align-items: center;
		min-height: 42rpx;
		justify-content: space-between;
	}

	.cu-form-group uni-picker .picker {
		line-height: 15px;
		font-size: 15px;
		text-overflow: ellipsis;
		white-space: nowrap;
		overflow: hidden;
		width: 100%;
		text-align: right;
	}

	.cu-form-group uni-picker::after {
		font-family: cuIcon;
		line-height: 15px;
		display: block;
		content: "\e6a3";
		position: absolute;
		font-size: 10px;
		color: #8799a3;
		width: 31px;
		text-align: center;
		top: 0;
		bottom: 0;
		right: -5px;
		margin: auto;
	}

	/* 	.cu-form-group uni-picker[data-v-077a1eae]::after {
		font-family: cuIcon;
		line-height: 15px;
		display: block;
		content: "\e6a3";
		position: absolute;
		font-size: 10px;
		color: #8799a3;
		width: 31px;
		text-align: center;
		top: 0;
		bottom: 0;
		right: -10px;
		margin: auto;
	} */

	/deep/ .uni-radio-input {
		width: 15px;
		height: 15px;
	}

	.nav-right-image {
		width: 40rpx;
		height: 40rpx;
		margin-left: 15px;
	}

	.padding-sm {
		padding: 5px;
	}

	.textStyle {
		font-size: 13px;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #101317;
		line-height: 22px;
	}

	.cu-form-group .title {
		text-align: justify;
		padding-right: 16rpx;
		font-size: 20rpx;
		position: relative;
		height: 38px;
		line-height: 38px;
	}

	.submit-icon {
		width: 118rpx;
		height: 118rpx;
		margin-top: 58rpx;
	}

	.red-tag {
		width: 100px;
		background-color: #fdc1be;
		color: #ff261b;
		text-align: center;
		margin-left: 5px;
		display: inline-block;
		padding: 7px;
		font-size: 20rpx;
		border-radius: 20px;
	}
</style>